/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.easygem.commandExecutor;

import java.util.ArrayList;
import java.util.List;
import net.tygzs.easygem.Main;
import net.tygzs.easygem.configuration.gem.RepairStoneConfig;
import static org.bukkit.Bukkit.getServer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * EasyGem指令执行类
 *
 * @author thtTNT
 */
public class EasyGem implements CommandExecutor
{

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[])
    {
        if (args.length == 0)
        {
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_give"));
            return true;
        }
        if (args[0].equalsIgnoreCase("give"))
        {
            if (args.length == 1)
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_give"));
                return true;
            }
            if (args[1].equalsIgnoreCase("RepairStone") || args[1].equalsIgnoreCase("rs"))
            {
                if (!sender.hasPermission("EasyGem.give.RepairStone"))
                {
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_noPermission"));
                    return true;
                }
                if (args.length == 2)
                {
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_give_RepairStone"));
                    return true;
                }
                ItemStack item = RepairStoneConfig.init();
                if (args.length == 3)
                {
                    try
                    {
                        int amount = Integer.parseInt(args[2]);
                        item.setAmount(amount);
                        Player player = (Player) sender;
                        player.getInventory().addItem(item);
                        String message = (String) Main.plugin.getPluginConfig().getMessage().get("Info_give_RepairStone");
                        message = message.replace("[player]", player.getName());
                        message = message.replace("[amount]", amount + "");
                        sender.sendMessage(message);
                        return true;
                    } catch (NumberFormatException e)
                    {
                        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("ErrorNumber"));
                        return true;
                    }
                }
                if (args.length == 4)
                {
                    try
                    {
                        int amount = Integer.parseInt(args[2]);
                        item.setAmount(amount);
                        Player player = getServer().getPlayer(args[3]);
                        if (player == null)
                        {
                            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Player_notFound"));
                            return true;
                        }
                        player.getInventory().addItem(item);
                        String message = (String) Main.plugin.getPluginConfig().getMessage().get("Info_give_RepairStone");
                        message = message.replace("[player]", player.getName());
                        message = message.replace("[amount]", amount + "");
                        sender.sendMessage(message);
                        return true;
                    } catch (NumberFormatException e)
                    {
                        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("EorrorNumber"));
                        return true;
                    }
                }
            }
        }
        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_EasyGem"));
        return true;
    }
}
